package com.bnz.manager.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bnz.common.utils.PageUtils;
import com.bnz.common.utils.Query;

import com.bnz.manager.dao.ContentDao;
import com.bnz.manager.entity.ContentEntity;
import com.bnz.manager.service.ContentService;


@Service("contentService")
public class ContentServiceImpl extends ServiceImpl<ContentDao, ContentEntity> implements ContentService {

    @Autowired
    //引入redis
    private StringRedisTemplate redisTemplate;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ContentEntity> page = this.page(
                new Query<ContentEntity>().getPage(params),
                new QueryWrapper<ContentEntity>()
        );

        return new PageUtils(page);
    }

    // @CacheEvict(value = "contentList",key = "'findByCategoryId2'")
    // @CacheEvict(value = "contentList",allEntries = true)   allEntries： 代表删除指定命名空间下的所有的key
    //如果想删除指定命名空间下的指定key，可以使用@Caching注解
    // @Caching(evict = {
    //         @CacheEvict(value =  "contentList",key = "'findByCategoryId2'"),
    //         @CacheEvict(value =  "contentList",key = "'18'")
    // })
    //原理：将指定key（CACHE_contentList::findByCategoryId2为key，删除redis中的数据，）
//    @CachePut(value = "contentList",key = "'findByCategoryId2'")
    @Override
    public ContentEntity inster(ContentEntity content) {
        //栓除redis缓存
//        redisTemplate.delete("contentList"+content.getCategoryId());
        this.save(content);
        return content;
    }
    // @CachePut(value = "content",key = "#content.id",unless = "#result == null")
    // @Cacheable(value = "content",key = "#content.id")        // 发现redis中有只做添加，不调用方法
    // @CachePut：首先，会查根据prefix + value + key找缓存中查找是否有对应的值，总是会调用方法，也可以用unless指定，如果方法返回值为null，则可不调用方法

    @Override
    @CacheEvict(value = "contentList",key = "'findByCategoryId2'")
    public ContentEntity  update(ContentEntity content) {
//栓除redis缓存
//        redisTemplate.delete("contentList"+content.getCategoryId());
        this.updateById(content);
        return content;
    }

    @CacheEvict(value = "contentList",key = "'findByCategoryId2'")
    @Override
    public void delete(List<String> ids) {
//        for (String id : ids) {
//            //查出对象
//            ContentEntity byId = this.getById(id);
//            //查出对象的分类id
//            String categoryId = byId.getCategoryId();
//            //删除缓存
//            redisTemplate.delete("contentList"+categoryId);
//        }
        this.removeByIds(ids);
    }
    @Cacheable(value = "contentList",key = "#id")
    @Override
    public ContentEntity findById(String id) {
        return this.getById(id);
    }

}